package com.example.a2209_shop.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.a2209_shop.R
import com.example.a2209_shop.adapter.CommentAdapter
import com.example.a2209_shop.databinding.FragmentCommentBinding
import com.example.a2209_shop.fgViewModel.CommentViewModel
import com.example.base.mvvm.BaseFragment


class CommentFragment : BaseFragment<FragmentCommentBinding,CommentViewModel>() {
    lateinit var commentAdapter: CommentAdapter
    var index = 0
    override fun initData() {
        viewModel.getSuccess.observe(this){
            if(it.code==200){
                commentAdapter.data.clear()
                commentAdapter.data.addAll(it.data)
                commentAdapter.notifyDataSetChanged()
            }
        }
        viewModel.getChildSuccess.observe(this){
            if(it.code==200){
                commentAdapter.data.addAll(index,it.data)
                commentAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        commentAdapter = CommentAdapter()
        binding.rvComment.adapter = commentAdapter
        binding.rvComment.layoutManager = LinearLayoutManager(context)
        viewModel.getComment(12)

        commentAdapter.setOnItemClickListener { adapter, view, position ->
            index = position
            viewModel.getChildComment(commentAdapter.data[position].rootid)
        }
    }

    override fun getViewModel(): Class<CommentViewModel> = CommentViewModel::class.java

    override fun getLayout(): Int = R.layout.fragment_comment

}